﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using WMS.FC.Domain.DomainObject.Menu;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Menu.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Menu
{
    public class CopyColumnService : ServiceBase, ICopyColumnService
    {
        protected readonly IMenuRepository menuRepository;

        public CopyColumnService(ILogger<ServiceBase> logger, IOptions<WmsSettings> options, IMapper mapper, IMenuRepository menuRepository) : base(logger, options, mapper)
        {
            this.menuRepository = menuRepository;
        }

        public async Task<ActionResult<List<ColumnDO>>> CopyColumn(string menuId, UserInfo userInfo)
        {
            var result = new ActionResult<List<ColumnDO>>("复制列");
            try
            {
                if (string.IsNullOrWhiteSpace(menuId))
                    return result.ArgumentError("菜单ID不能为空");

                var menuPO = await menuRepository.GetMenuWithParentMenu(Convert.ToInt64(menuId));
                if (menuPO == null)
                    return result.BusinessError($"ID:{menuId},菜单数据未找到");

                if (menuPO.Parent == null || string.IsNullOrWhiteSpace(menuPO.Parent.ConfigJson))
                {
                    result.Data = new List<ColumnDO>();
                }
                else
                {
                    result.Data = JsonConvert.DeserializeObject<List<ColumnDO>>(menuPO.Parent.ConfigJson);
                }
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
